/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.ccut.titan.component.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import edu.ccut.titan.component.IBundleInfo;
import edu.ccut.titan.component.IComponentInfo;
import edu.ccut.titan.component.TitanComponentException;

/**
 * {@link IBundleInfo} implementation
 * 
 * @see IBundleInfo
 * @author LeslieGu
 */
public class TitanBundleInfo implements IBundleInfo {

	protected static Logger logger = Logger.getLogger(IBundleInfo.class.getName());
	
	private String bundlesSymbolicName;

	private Map<String, IComponentInfo> componentInfoMap;
	
	private String webRootPath = null;

	public TitanBundleInfo(String bundlesSymbolicName) {
		this.bundlesSymbolicName = bundlesSymbolicName;
		this.componentInfoMap = new HashMap<String, IComponentInfo>();
	}

	public void addComponentInfo(IComponentInfo componentInfo)
			throws TitanComponentException {
		
		validateComponentInfoKey(componentInfo);
		this.componentInfoMap.put(componentInfo.getKey(), componentInfo);
		
		logger.debug("add componentInfo \"" + componentInfo.getKey() + "\" from " + this.bundlesSymbolicName + " bundle");
	}

	public void addComponentInfos(Collection<IComponentInfo> componentInfos)
			throws TitanComponentException {
		if (componentInfos != null) {

			for (IComponentInfo componentInfo : componentInfos) {
				validateComponentInfoKey(componentInfo);
			}

			for (IComponentInfo componentInfo : componentInfos) {
				this.addComponentInfo(componentInfo);
			}
		}
	}

	public String getBundleSymbolicName() {
		return this.bundlesSymbolicName; 
	}

	public Collection<IComponentInfo> getComponentInfos() {
		return this.componentInfoMap.values(); 
	}

	public Set<String> getComponentKeys() {
		return this.componentInfoMap.keySet();
	}

	public IComponentInfo getComponentInfo(String componentKey) {
		return this.componentInfoMap.get(componentKey); 
	}

	private void validateComponentInfoKey(IComponentInfo componentInfo)
			throws TitanComponentException {
		if (componentInfo.getKey() == null) {
			throw new TitanComponentException(
					"add componentInfo failed, because the componentInfo key is null");
		}

		if (this.componentInfoMap.containsKey(componentInfo.getKey())) {
			throw new TitanComponentException(
					"add componentInfo failed, because the componentInfo key ["
							+ componentInfo.getKey() + "] is already existed");
		}
	}

	public String getWebRootPath() {
		return webRootPath;
	}

	public void setWebRootPath(String webRootPath) {
		this.webRootPath = webRootPath;
	}
}
